package com.example.wuqujingdianphone.fragment.bookcity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.example.wuqujingdianphone.R;
import com.example.wuqujingdianphone.activity.LoginActivity;
import com.example.wuqujingdianphone.activity.MainActivity;
import com.example.wuqujingdianphone.api.ApiConfig;
import com.example.wuqujingdianphone.api.GlobalConfig;
import com.example.wuqujingdianphone.base.BaseFragment;
import com.example.wuqujingdianphone.bean.LoginBean;
import com.example.wuqujingdianphone.utils.MD5Util;
import com.example.wuqujingdianphone.utils.NiceDialogUtils;
import com.example.wuqujingdianphone.utils.SPUtil;
import com.example.wuqujingdianphone.utils.ToastUtil;
import com.example.wuqujingdianphone.view.CommonWebView;
import com.example.wuqujingdianphone.view.X5WebView;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by 29083 on 2018/4/10.
 * <p>
 * 书城--网页展示
 */

public class WebViewFragment extends BaseFragment {
    private static final String TAG = "WebViewFragment";
    @BindView(R.id.fl_content)
    FrameLayout mFlContent;
    /* @BindView(R.id.webView)
     X5WebView mWebView;*/
    private String type;
    private boolean isFirstLoadDate;


    public static WebViewFragment getWebViewFragment(String type) {
        WebViewFragment fragment = new WebViewFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_book_shop_webview, null);
        type = getArguments().getString("type");
        return view;
    }

    @Override
    protected void initData() {
        Log.i(TAG, "initData: ");
        isFirstLoadDate = true;


    }

    @Override
    protected void lazyLoad() {
        Log.i(TAG, "lazyLoad: type=" + type);
        if (isFirstLoadDate) {
            isFirstLoadDate = false;
            initView(type);
        }
    }

    private String App_key = "fed82efeba2963ddb349a351726a33b2";
    private String Identify_key = "3d6c567b5ea821a17df9ecd26a42a98e";
    private int Type_id = 1;
    private String userId;
    private String sign_code;

    @SuppressLint("ClickableViewAccessibility")
    private void initView(String type) {

        final DisplayMetrics dm = new DisplayMetrics();
        WindowManager manager = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
        assert manager != null;
        manager.getDefaultDisplay().getMetrics(dm);

        userId = SPUtil.getString(getActivity(), GlobalConfig.ACCOUNT_KEY, "");
        Log.i(TAG, "initView: userId=" + userId);
        //ViseHttp.CONFIG().baseUrl("http://api.183read.cc");

        //启用硬件加速
        try {
            if (Integer.parseInt(android.os.Build.VERSION.SDK) >= 11) {
                getActivity().getWindow().setFlags(
                        android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
                        android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
            }
        } catch (Exception e) {
        }


        if (type != null) {
            String webUrl = "";
            switch (type) {
                case "不忘初心牢记使命":
                    final CommonWebView mWebView2 = new CommonWebView(getActivity(), null);
                    mFlContent.addView(mWebView2);
                    webUrl = "http://cpc.people.com.cn/xuexi/";
                    mWebView2.loadUrl(webUrl);
                    mWebView2.setOnKeyListener(new View.OnKeyListener() {
                        @Override
                        public boolean onKey(View v, int keyCode, KeyEvent event) {
                            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                                if (keyCode == KeyEvent.KEYCODE_BACK && mWebView2.canGoBack()) {
                                    mWebView2.goBack();   //后退
                                    return true;    //已处理     返回true表示被处理否则返回false
                                }
                            }
                            return false;
                        }
                    });
                    break;
                case "期刊":
                    final CommonWebView mWebView = new CommonWebView(getActivity(), null);
                    //final X5WebView mWebView = new X5WebView(getActivity(), null);
                    mFlContent.addView(mWebView);
                    //webUrl = "http://vertical.183read.cc/wqjd.html";
//                    webUrl = "http://yuedu.183read.cc/organization/wqjd.html?user_id=" + userId;
//                    webUrl = "http://vertical.183read.cc/wqjd.html";
                    webUrl = "http://vertical.591adb.cn/wqjd.html";
                   // mWebView.requestDisallowInterceptTouchEvent(true);
                    mWebView.loadUrl(webUrl);

                    mWebView.setOnKeyListener(new View.OnKeyListener() {
                        @Override
                        public boolean onKey(View v, int keyCode, KeyEvent event) {
                            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                                if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
                                    mWebView.goBack();   //后退
                                    return true;    //已处理     返回true表示被处理否则返回false
                                }
                            }
                            return false;
                        }
                    });
                    break;
                case "报纸":
                    final CommonWebView mWebView8= new CommonWebView(getActivity(), null);
                    mFlContent.addView(mWebView8);
//                    webUrl = "http://wenqujingdian.com/index.php/Baozhi";
                    webUrl = "http://www.wenqujingdian.com/index.php/Baozhi/index/lm/517.shtml";
                    mWebView8.loadUrl(webUrl);
                    mWebView8.setOnKeyListener(new View.OnKeyListener() {
                        @Override
                        public boolean onKey(View v, int keyCode, KeyEvent event) {
                            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                                if (keyCode == KeyEvent.KEYCODE_BACK && mWebView8.canGoBack()) {
                                    mWebView8.goBack();   //后退
                                    return true;    //已处理     返回true表示被处理否则返回false
                                }
                            }
                            return false;
                        }
                    });
                    break;
                case "书友会":
                    final CommonWebView mWebView3 = new CommonWebView(getActivity(), null);
                    mFlContent.addView(mWebView3);
//                    webUrl = "http://taiyuan.zxhsd.com";
                    webUrl = "http://www.sxtybook.com/";
                    mWebView3.loadUrl(webUrl);
                    mWebView3.setOnKeyListener(new View.OnKeyListener() {
                        @Override
                        public boolean onKey(View v, int keyCode, KeyEvent event) {
                            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                                if (keyCode == KeyEvent.KEYCODE_BACK && mWebView3.canGoBack()) {
                                    mWebView3.goBack();   //后退
                                    return true;    //已处理     返回true表示被处理否则返回false
                                }
                            }
                            return false;
                        }
                    });
                    break;
            }
        }
    }


}
